package com.zsck.pos.view;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;


public class RefreshListView extends ListView implements OnScrollListener, AdapterView.OnItemClickListener {
	private static final int STATE_PULL_REFRESH 	= 0;	//下拉刷新
	private static final int STATE_RELEASE_REFRESH	= 1;	//松开刷新
	private static final int STATE_REFRESHING		= 2;	//正在刷新
	private float startY = 0;			//开始触摸位置
	private View footer;
	private int fHeight;
	private View header;				//HeaderView
	private int hHeight;				//HeaderView的高度
	private int state = 0;
	private TextView tv_title;
	private TextView tv_time;
	private ImageView iv_pull;
	private ProgressBar pb_refresh;
	private RotateAnimation animDown;
	private RotateAnimation animUp;
	private OnRefreshListener listener;
	private boolean loading;			//是否正在加载中
	private boolean pull;				//是否拉动,用来在touch事件中返回true拦截事件传递
	public RefreshListView(Context context) {
		super(context);
	}
	public RefreshListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initFooterView();
	}
	
	public RefreshListView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initFooterView();
	}


	private void initFooterView(){
		setOnScrollListener(this);
	}


	public void setOnRefreshListener(OnRefreshListener listener){
		this.listener = listener;
	}
	public void refreshComplete(boolean success){
		if(loading){
			loading = false;
		}else{
			state = STATE_PULL_REFRESH;
		}
	}
	public interface OnRefreshListener{
		public void onLoadMore();
	}

	
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if(scrollState == SCROLL_STATE_IDLE || scrollState == SCROLL_STATE_FLING){
			if(getLastVisiblePosition() == getCount() - 1 && !loading){
				loading = true;
				setSelection(getBottom());
				if(listener != null){
					listener.onLoadMore();
				}
			}
		}
	}
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		
	}
	OnItemClickListener mItemClickListener;

	@Override
	public void setOnItemClickListener(
			OnItemClickListener listener) {
		super.setOnItemClickListener(this);

		mItemClickListener = listener;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if(position >= getHeaderViewsCount() && position < getCount() - getFooterViewsCount()){
			if (mItemClickListener != null ) {
				mItemClickListener.onItemClick(parent, view, position
						- getHeaderViewsCount(), id);
			}
		}
		
		
	}
	
}
